/*
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 *
 */

import * as React from "react";
import { connect } from "react-redux";
import { IRootState, Dispatch } from "../../../../store";
import { Form, Table, Input, Button } from "antd";

import { FormComponentProps } from "antd/lib/form";
import TitleCard from "src/components/TitleCard";
import moment from "moment";

const mapStateToProps = ({ product, loading }: IRootState) => ({
  product,
  loading: loading.effects.product.getProductList as any
});
const mapDispatchToProps = ({ product }: Dispatch) => ({
  getProductList: (payload: any) => product.getProductList(payload)
});

type connectedProps = ReturnType<typeof mapStateToProps> &
  ReturnType<typeof mapDispatchToProps>;

interface IState {
  visible: boolean;
  edit: boolean;
  searchValue: string;
  currPage: number;
}

interface IProps extends connectedProps {
  loading: boolean;
}

class ProductList extends React.Component<IProps & FormComponentProps, IState> {
  public state = {
    visible: false,
    edit: false,
    searchValue: "",
    currPage: 0
  };

  public componentDidMount = async () => {
    const { getProductList } = this.props;
    getProductList({
      page: 0,
      name: ""
    });
  };
  public handleSearch = async () => {
    const { getProductList } = this.props;
    getProductList({
      page: 0,
      name: this.state.searchValue
    });
  };

  public inputChange = (e: any) => {
    this.setState({
      searchValue: e.target.value
    });
  };

  public changePage(pageNumber: any, pageSize: any, props: any) {
    const { getProductList } = this.props;
    const payload = {
      name: this.state.searchValue,
      page: pageNumber - 1,
      size: pageSize
    };
    this.setState({
      currPage: pageNumber
    });

    getProductList(payload);
  }

  public tableRender() {
    const { productList } = this.props.product;
    const { pageTotal } = this.props.product;
    const column = [
      {
        title: "序号",
        dataIndex: "seq",
        render: (text: any, record: any, index: any) => {
          return index + 1;
        }
      },
      {
        title: "产品名称",
        dataIndex: "name"
      },
      {
        title: "产品描述",
        dataIndex: "description"
      },
      {
        title: "状态",
        dataIndex: "status",
        render: (text: any, record: any, index: any) => {
          if (String(text) === "0") {
            return "未启用";
          } else {
            return "启用";
          }
        }
      },
      {
        title: "创建时间",
        dataIndex: "creatorTime",
        render: (text: any, record: any, index: any) => {
          return (
            <span>
              {text ? moment(text).format("YYYY-MM-DD HH:mm:ss") : " - -"}
            </span>
          );
        }
      },
      {
        title: "更新时间",
        dataIndex: "updateTime",
        render: (text: any, record: any, index: any) => {
          return (
            <span>
              {text ? moment(text).format("YYYY-MM-DD HH:mm:ss") : " - -"}
            </span>
          );
        }
      }
    ];

    return (
      <Table
        dataSource={productList}
        columns={column}
        rowKey={"id"}
        pagination={{
          current: this.state.currPage,
          total: pageTotal,
          onChange: (page, size) => this.changePage(page, size, this.props)
        }}
      />
    );
  }

  public render() {
    return (
      <div className="message-container" style={{ paddingTop: 32 }}>
        <div style={{ marginLeft: "20px" }}>
          <b>产品名称:</b>
          <Input
            style={{ width: "200px", marginLeft: "10px" }}
            onChange={(e: any) => this.inputChange(e)}
          />
          <Button
            type="primary"
            onClick={this.handleSearch}
            style={{ marginLeft: "10px" }}
          >
            查询
          </Button>
        </div>

        <TitleCard style={{ paddingTop: 32 }}>{this.tableRender()}</TitleCard>
      </div>
    );
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Form.create<IProps & FormComponentProps>({})(ProductList));
